<!--
Date: 2025-08-13
Copyright (c) 2025 Industrial Software Feature Database
-->
<template>
  <el-dialog v-model="props.dialogVisible" :title="'查看分析报告'" width="1600px" append-to-body :before-close="closeDialog">
    <div v-loading="loading">
      <h3 class="compare-title"><span class="title">对比对象</span></h3>
      <div class="compare-card">
        <el-row :gutter="6">
          <el-col :span="6" class="custom-margin">
            <div style="display: flex; justify-content: center">
              <el-card shadow="never" class="scroll-card">
                <template #header>
                  <div class="card-header">
                    <span class="title">需要对比的模型文件</span>
                  </div>
                </template>
                <el-form ref="referencedObjectRef" :model="referencedObject" size="small" label-width="120">
                  <el-form-item label="机型：" prop="modelNumber">
                    <span>{{ referencedObject.modelNumber }}</span>
                  </el-form-item>
                  <el-form-item label="批次号：" prop="batchNumber">
                    <span>{{ referencedObject.batchNumber }}</span>
                  </el-form-item>
                  <el-form-item label="部件名称：" prop="sectionNumbers">
                    <span>{{ referencedObject.sectionNumbers }}</span>
                  </el-form-item>
                  <el-form-item label="轮次：" prop="filePath">
                    <span>{{ referencedObject.roundNumber }}</span>
                  </el-form-item>
                  <el-form-item label="备注：" prop="filePath">
                    <span>{{ referencedObject.remarks }}</span>
                  </el-form-item>
                </el-form>
              </el-card>
            </div>
          </el-col>
          <el-col :span="2">
            <div class="bold-text">
              <div class="vs">VS</div>
            </div>
          </el-col>
          <el-col :span="16">
            <div class="scroll-container">
              <el-card v-for="(comparedObject, index) in comparedObjects" :key="index" shadow="never" class="scroll-card">
                <template #header>
                  <div class="card-header">
                    <span class="title">对比对象{{ index + 1 }}</span>
                  </div>
                </template>
                <el-form :ref="(el) => (comparedObjectRefs[index] = el)" :model="comparedObject" size="small" label-width="120">
                  <el-form-item label="机型：" prop="modelNumber">
                    <span>{{ comparedObject.modelNumber }}</span>
                  </el-form-item>
                  <el-form-item label="批次号：" prop="batchNumber">
                    <span>{{ comparedObject.batchNumber }}</span>
                  </el-form-item>
                  <el-form-item label="部件名称：" prop="sectionNumbers">
                    <span>{{ comparedObject.sectionNumbers }}</span>
                  </el-form-item>
                  <el-form-item label="轮次：" prop="filePath">
                    <span>{{ comparedObject.roundNumber }}</span>
                  </el-form-item>
                  <el-form-item label="备注：" prop="filePath">
                    <span>{{ comparedObject.remarks }}</span>
                  </el-form-item>
                </el-form>
              </el-card>
            </div>
          </el-col>
        </el-row>
      </div>
      <h3 class="compare-title"><span class="title">对比报告</span></h3>
      <el-tabs v-model="activeName" @tab-change="handleTabChange">
        <el-tab-pane v-for="item in dataList" :key="item.itemName" :name="item.itemName" :label="item.itemName">
          <el-card v-if="activeName === item.itemName" style="padding: 20px 50px">
            <div
              v-for="(params, index) in item.elementGroups"
              :id="`comparingChart${item.matchKey}${index + 1}`"
              :key="`CompareChart${item.matchKey}${index + 1}`"
              :ref="`compareChart${item.matchKey}${index + 1}`"
              style="width: 100%; height: 400px; margin-bottom: 100px"
            >
              <div style="display: flex; justify-content: space-between">
                <span style="font-size: 17px; font-weight: 700">{{ params.groupName }}</span>
                <el-button type="text" @click="handleShowData(params)">查看数据详情</el-button>
              </div>
            </div>
          </el-card>
        </el-tab-pane>
      </el-tabs>
    </div>
    <template #footer>
      <div class="dialog-footer" style="text-align: center">
        <el-button size="small" @click="closeDialog">返回</el-button>
      </div>
    </template>
  </el-dialog>
  <AnalysisData v-if="dataVisible" :dataVisible="dataVisible" :rowData="row" @handleEvent="handleEvent" />
</template>

<script setup>
import { getCompareTaskDetail } from '@/api/dataAcquisition';
import AnalysisData from './AnalysisData.vue';
import VChart from '@visactor/vchart';
const CONTAINER_ID = ref('');
const emit = defineEmits(['handleEvent']);
const loading = ref(false);
const props = defineProps(['dialogVisible', 'rowData']);
const comparedObjects = ref([]);
const comparedObjectRefs = ref([]);
const referencedObjectRef = ref(null);
const activeName = ref('first');
const referencedObject = ref({});
const dataList = ref([]);
const row = ref([]);
const dataVisible = ref(false);

const handleTabChange = (newTab) => {
  activeName.value = newTab;
  dataList.value.forEach((params) => {
    if (params.itemName === newTab) {
      const chart = params.elementGroups;
      nextTick(() => {
        chart.forEach((item, index) => {
          renderChart(item, params.matchKey, index + 1);
        });
      });
    }
  });
};

const renderChart = async (data, matchKey, index) => {
  await nextTick();
  let allDataDetailList = data.dataList.flatMap((data) =>
    data.dataDetailList
      .sort((a, b) => a.xindex - b.xindex) // 按照xindex从小到大排序
      .flatMap((detail) => {
        let minDetail = { ...detail, type: data.name + '最小值', value: detail.minData };
        let maxDetail = { ...detail, type: data.name + '最大值', value: detail.maxData };
        return [minDetail, maxDetail];
      })
  );
  if (matchKey && index) {
    CONTAINER_ID.value = `comparingChart${matchKey}${index}`;
  }
  const spec = {
    type: 'line',
    data: {
      values: allDataDetailList
    },
    title: {
      visible: true,
      text: '轴力/N'
    },
    xField: 'xindex',
    yField: 'value',
    seriesField: 'type',
    invalidType: 'link',
    lineLabel: { visible: true },
    legends: [{ visible: true, position: 'middle', orient: 'bottom' }]
  };
  let vchart = new VChart(spec, { dom: CONTAINER_ID.value });
  vchart.renderSync();
  window['vchart'] = null;
  window['vchart'] = vchart;
};

const handleShowData = (params) => {
  dataVisible.value = true;
  row.value = params;
};

const handleEvent = () => {
  dataVisible.value = false;
};

const closeDialog = () => {
  emit('handleEvent', { type: 'cancel' });
};

onMounted(() => {
  getCompareTaskDetail({ id: props.rowData._id }).then((res) => {
    if (res.code === 200) {
      const comparedObjectsData = JSON.parse(JSON.stringify(res.data.comparedObjects));
      const resultItemsData = JSON.parse(JSON.stringify(res.data.resultItems));
      referencedObject.value = comparedObjectsData[0];
      comparedObjectsData.shift();
      comparedObjects.value = comparedObjectsData;
      dataList.value = resultItemsData;
      nextTick(() => {
        activeName.value = dataList.value[0].itemName;
        dataList.value[0].elementGroups.forEach((item, index) => {
          renderChart(item, dataList.value[0].matchKey, index + 1);
        });
      });
    }
  });
});
</script>

<style lang="scss" scoped>
.scroll-container {
  width: 950px;
  overflow-x: auto; /* 激活横向滚动 */
  white-space: nowrap; /* 防止内容换行 */
  margin-bottom: 20px;
  margin-left: 40px;
}

.scroll-card {
  width: 304px;
  margin-right: 10px; /* 卡片之间的间距 */
  display: inline-block; /* 将卡片设置为行内块元素以实现横向排列 */
}

.radio-choose {
  padding: 10px 0;
}
.compare-card {
  .color-mark {
    padding: 10px 0;
    display: flex;
    justify-content: space-around;
    .color-mark__item {
      padding: 5px 10px;
      font-size: 14px;
      &.modified {
        background: #e6a23c;
        // color: #fff;
      }
      &.added {
        background: rgba(255, 0, 0, 0.3);
        // color: #fff;
      }
      &.removed {
        background: rgba(194, 128, 255, 1);
        // color: #fff;
      }
    }
  }
  .title {
    font-weight: 600;
    font-size: 16px;
  }
  .bold-text {
    display: flex;
    justify-content: center;
    height: 100%;
    flex-direction: column;
    align-items: center;
    .vs {
      height: 80px;
      width: 80px;
      line-height: 80px;
      text-align: center;
      font-weight: bold;
      font-size: 40px;
      color: #ffffff;
      background: #409eff;
      border-radius: 10px;
      text-shadow: 2px 2px 4px #000000;
    }
  }
}
.compare-title {
  .title {
    font-size: 18px;
    font-weight: 500;
    color: rgba(0, 0, 0, 0.85);
    &::before {
      content: '';
      display: inline-block;
      width: 5px;
      height: 16px;
      margin-right: 5px;
      background-color: #409eff;
      vertical-align: middle;
      margin-top: -3px;
    }
  }
}
.compare-result {
  ul,
  li {
    list-style: none;
    margin: 0;
    padding: 0;
  }
  .modified {
    background: #e6a23c;
    color: #fff;
  }
  .added {
    background: rgba(255, 0, 0, 0.3);
  }
  .removed {
    background: rgba(194, 128, 255, 1);
  }
}
.compare-table__header {
  display: flex;
  align-items: center;
  // border-left: 1px solid #ebeef5;
  // border-bottom: 1px solid #ebeef5;
  font-weight: 600;
  font-size: 14px;
  list-style: none;
  padding: 0;
  margin: 0;
  .th {
    // flex: 1;
    text-align: center;
    padding: 5px 10px;
    border-right: 1px solid #ebeef5;
    border-top: 1px solid #ebeef5;
    border-bottom: 1px solid #ebeef5;
    background: #f8f8f9;
    color: #515a6e;
  }
}
.single {
  .th,
  .td {
    &:nth-child(1) {
      width: 700px;
      border-left: 1px solid #ebeef5;
    }
    &:nth-child(2) {
      width: 500px;
    }
    &:nth-child(3) {
      width: 120px;
    }
  }
}
.total {
  .th,
  .td {
    &:nth-child(1) {
      width: 400px;
      border-left: 1px solid #ebeef5;
    }
    &:nth-child(2) {
      width: 170px;
    }
    &:nth-child(3) {
      width: 90px;
    }
    &:nth-child(4) {
      width: 400px;
    }
    &:nth-child(5) {
      width: 170px;
    }
    &:nth-child(6) {
      width: 90px;
    }
  }
}
.compare-table__content {
  display: flex;
  // align-items: center;
  // border-top: 1px solid #ebeef5;
  // border-bottom: 1px solid #ebeef5;
  font-weight: 400;
  font-size: 14px;
  list-style: none;
  padding: 0;
  margin: 0;
  // white-space: pre-line;
  .td {
    // flex: 1;
    text-align: left;
    padding: 5px 10px;
    border-right: 1px solid #ebeef5;
    height: 50px;
    border-bottom: 1px solid #ebeef5;
    white-space: pre-wrap;
  }
}
.custom-tabs-label {
  display: flex;
  align-items: center;
  i {
    margin-right: 3px;
  }
}
</style>
